package com.panshi.controller;

import com.alibaba.fastjson.JSONObject;
import com.panshi.IZxArticleApi;
import com.panshi.config.MyControllerReturnUtls;
import com.panshi.domain.*;
import com.panshi.domain.vo.PageBean;
import com.panshi.domain.vo.QueryVO;
import com.panshi.domain.vo.ZxResult;
import com.panshi.service.ZxArticleService;
import com.panshi.service.ZxCommentService;
import com.panshi.vo.CodeMsgConstant;
import com.panshi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资讯㔿审核以通过
 * 前端控制器
 * </p>
 *
 * @author zhy
 * @since 2020-07-29
 */
@RestController
@Slf4j
@Api(tags = "资讯文章")
@CrossOrigin
@RequestMapping("/api/article")
public class ZxArticleController implements IZxArticleApi {
    @Resource
    private ZxArticleService zxArticleService;
    @Resource
    private ZxCommentService zxCommentService;

    @ApiOperation("资讯首页随机查询资讯")
    @GetMapping(value = "/articleList")
    public List<ArticleVo> articleList() {
        return zxArticleService.articleList();
    }


    @ApiOperation("咨询推荐，热点，最新查询")
    @PostMapping(value = "/articleListCondition")
    public List<ArticleVo> articleListCondition(@RequestBody TypeVo typeVo) {
        return zxArticleService.articleListCondition(typeVo);
    }


    @ApiOperation("首页（最新创作）和咨询类型（其它看点）查询")
    @GetMapping(value = "/articleRandomQuerySix")
    public List<ArticleVo> articleRandomQuerySix() {
        return zxArticleService.articleRandomQuerySix();
    }


    @ApiOperation("资讯详情相关推荐")
    @PostMapping(value = "/articleDetaiRecommendlList")
    public List<ArticleVo> articleDetaiRecommendlList(@RequestBody TypeVo typeVo) {
        return zxArticleService.articleDetaiRecommendlList(typeVo);
    }


    @ApiOperation(value = "根据ID查询资讯详情")
    @PostMapping(value = "/zxArticle/getbayid")
    public Map ZxBayId(@RequestBody PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        ZxArticle zxArticle = zxArticleService.getZxArticleBayId(pageBean.getZxId());
        List<ZxComment> comments= zxCommentService.getCommentBayZxId(pageBean);
        if (null != zxArticle) {
            zxArticleService.read(pageBean.getZxId()); /*进入详情，阅读数增加*/
            map.put("zxArticle", JSONObject.toJSONString(zxArticle));
            map.put("comments",JSONObject.toJSONString(comments));
            return map;
        } else {
            return null;
        }
    }

    @ApiOperation("根据ID查询资讯详情(admin)")
    @GetMapping(value = "/getbayid/{id}")
    public R adminZxBayId(@PathVariable("id") Integer id) {
        ZxArticle zxArticle = zxArticleService.getZxArticleBayId(id);
        if (null != zxArticle) {
            return R.ok(CodeMsgConstant.CODE_0).data("zxArticle", zxArticle);
        } else {
            return R.error(CodeMsgConstant.CODE_1).data(null);
        }
    }

    @ApiOperation("全部资讯")
    @PostMapping(value = "/pageZxList")
    public R pageZxList(@RequestBody QueryVO queryVO) {
        List<ZxArticle> zxArticles = zxArticleService.pageZxList(queryVO);
        System.out.println(zxArticles.size());
        if (zxArticles.size() <= 0) {
            return R.error(CodeMsgConstant.CODE_1);
        } else {
            return R.ok(CodeMsgConstant.CODE_0).data("data", zxArticles).data("count",zxArticleService.count(queryVO));
        }

    }

    @ApiOperation("下架 上架")
    @PutMapping(value = "/zxArticle/deleteby")
    public R updataStatus(@RequestBody ZxArticle zxArticle) {
        int result = zxArticleService.updataStatus(zxArticle);
        if (result > 0) {
            return R.ok(CodeMsgConstant.CODE_0).data("zxArticle", zxArticle);
        } else {
            return R.error(CodeMsgConstant.CODE_1).data(null);
        }

    }


    @ApiOperation("根据类型查询资讯")
    @PostMapping(value = "/zxArticle/zxgettype")
    public Map<String, Object> getZxArticleBayType(@RequestBody PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        int num = zxArticleService.typeCount(pageBean.getTid());//根据资讯类型查询分页总数
        List<ZxArticle> zxArticles = zxArticleService.getZxArticleBayType(pageBean);//根据类型查询资讯
        List<ZxArticle> zxArticleType = zxArticleService.zxTypeCorrelation(pageBean.getTid());//查询相关推荐资讯
        map.put("code", 0);
        map.put("data", zxArticles);
        map.put("correlation", zxArticleType);
        map.put("total", num);
        return map;
    }


    @ApiOperation("根据ID查询分类资讯详情")
    @GetMapping(value = "/zxTypeBayId/{id}")
    public Map<String, Object> zxTypeBayId(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        ZxArticle zxArticle = zxArticleService.getZxArticleBayId(id);
        if (null != zxArticle) {
            List<ZxArticle> correlation = zxArticleService.zxTypeBy(zxArticle.getTypeId());//分类详情页面相关推荐
            zxArticleService.read(id); /*进入详情，阅读数增加*/
            map.put("zxArticle", zxArticle);
            map.put("correlation", correlation);
        }
        return map;
    }


    /*@ApiOperation("热门资讯")
    @PostMapping(value ="/zxArticle/hot")
    public Map hotZxArticle(@RequestBody PageBean pageBean){
        Map <String,Object> map = new HashMap<>();
        pageBean.setSign(1);
        List<ZxArticle>  zxArticles = zxArticleService.hotZxArticle(pageBean);
        log.info("查询结果****{}",zxArticles);
        map.put("code",0);
        map.put("count",pageBean.getTotalNum());
        map.put("data",zxArticles);
        return map;
    }*/


     /*@ApiOperation("新增审核通过资讯")
    @PostMapping(value = "/zxArticle/create")
    public ZxResult addZx(@RequestBody ZxUncheck zxUncheck) {
        //站内消息
        int result = zxArticleService.addZxArticle(zxUncheck);
        if (result > 0) {
            zxArticleService.addZxArticleSearch(zxArticleService.getZxArticleBayId(zxUncheck.getId()));//同步搜索表
            return new ZxResult(200, "操作数据库成功", result);
        }
        return new ZxResult(400, "操作数据库失败", null);
    }*/

 /*   @ApiOperation("最新资讯")
    @PostMapping(value = "/zxArticle/newest")
    public Map newestArticle(@RequestBody PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        pageBean.setSign(1);
        List<ZxArticle> zxArticles = zxArticleService.newestArticle(pageBean);
        log.info("查询结果****{}", zxArticles);
        map.put("code", 0);
        map.put("count", zxArticleService.count());
        map.put("data", zxArticles);
        return map;
    }*/


    @ApiOperation("大家都在看")
    @GetMapping(value = "/zxArticle/Look")
    public ZxResult everyoneLook() {
        List<ZxArticle> zxArticles = zxArticleService.everyoneLook();
        return MyControllerReturnUtls.ControllerReturn(zxArticles);
    }


    @ApiOperation("专家推荐")
    @GetMapping(value = "/zxArticle/expert")
    public ZxResult expertRecommend() {
        List<ZxArticle> zxArticles = zxArticleService.expertRecommend();
        return MyControllerReturnUtls.ControllerReturn(zxArticles);
    }


    @ApiOperation("查询资讯页类型")
    @GetMapping(value = "/zxArticle/type")
    public ZxResult queryType() {
        List<ThType> zxTypes = zxArticleService.queryType();
        return MyControllerReturnUtls.ControllerReturn(zxTypes);
    }


    @ApiOperation("用户删除")
    @DeleteMapping("/zxArticle/userdelt/{id}")
    public ZxResult userDelete(@PathVariable("id") Integer id) {
        int result = zxArticleService.userDelete(id);
        return MyControllerReturnUtls.ControllerReturn(result);
    }

    @ApiOperation("添加用户收藏资讯")
    @PostMapping("/zxArticle/addcollect")
    public R addCollect(@RequestBody ZxArticle zxArticle) {
        if(zxArticleService.queryUserId(zxArticle)==0){
            int result = zxArticleService.addCollect(zxArticle);
            if (result > 0) {
                zxArticleService.collect(zxArticle.getId());
                return R.ok(CodeMsgConstant.CODE_0);
            }
            return R.error(CodeMsgConstant.CODE_1);
        }
        return R.error(CodeMsgConstant.CODE_1);
    }




}

